var util = require("../../../utils/util.js"),
  {
    api
  } = require("../../../utils/api.js"),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname: '',
    mobile: '',
    complaint: '',
    suggestion: '',
    kjList: ['医疗质量', '服务态度', '其它'],
    kjActive: '',
    institution_name: '',
    multiArray: [],
    cityIndex: [0, 0],
    cityList: [],
    store_id:null,
    cityList:[],
    storeAllList:[],
    textNum:500
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu()
    this.getUsers();
    this.patientStortList()
  },
  async getUsers() {
    const data = await app.getUsers();
    this.setData({
      nickname: data && data.userInfo ? data.userInfo.username : '',
      mobile: data && data.userInfo ? data.userInfo.mobile : '',
    })
  },
  getUserName(e) {
    this.setData({
      nickname: e.detail.value
    })
  },
  getUserMobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  getTs(e) {
    if (e.detail.value.length <= this.data.textNum) {
      this.setData({
        complaint: e.detail.value
      })
    }
  },
  getjy(e) {
    if (e.detail.value.length <= this.data.textNum) {
      this.setData({
        suggestion: e.detail.value
      })
    }
  },
  getKj(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    if ((this.data.complaint.length + name.length) >= 200) return false;
    let complaint = this.data.complaint + name;
    this.setData({
      complaint,
      kjActive: index
    })
  },
  async getAreaList() {
    let {
      data
    } = await api.getAreaList();
    let areaList = Array.isArray(data.area_list) ? data.area_list : [],
      cityList = [],
      storeAllList = [];
    areaList.forEach(item => {
      let child = item.child && Array.isArray(item.child) ? item.child : [];
      child.forEach(val => {
        if (util.regularConfig.isString(val)) {
          cityList.push(val)
          storeAllList.push({
            area: val,
            storeList: []
          })
        }
      })
    })
    let institution_name = '',
      cityIndex = [0, 0];
    storeAllList.forEach(item => {
      this.data.agencyList.forEach(val => {
        if (item.area === val.area) {
          item.storeList.push({
            name: val.name,
            storeId: val.id
          })
        }
      })
    })
    this.setData({ 
      cityList,
      storeAllList,
      institution_name,
      cityIndex,
      multiArray: [cityList, (storeAllList.length > cityIndex[0] ? storeAllList[cityIndex[0]].storeList.map(o => o.name) : [])]
    })
  },
  bindMultiPickerColumnChange(e) {
    let sIndex = e.detail.value,
      column = e.detail.column,
      storeAllList = this.data.storeAllList;
    if (column == 0) {
      let areaList = []
      if (storeAllList[sIndex].storeList && Array.isArray(storeAllList[sIndex].storeList) && storeAllList[sIndex].storeList.length > 0) {
        areaList = storeAllList[sIndex].storeList.map(o => o.name);
      }
      this.setData({
        multiArray: [this.data.cityList, areaList],
        cityIndex: [sIndex, 0]
      })
    } else if (column == 1) {
      this.data.cityIndex[1] = sIndex;
      this.setData({
        cityIndex: this.data.cityIndex
      })
    }
  },
  cityChange() {
    const {
      storeAllList,
      cityIndex
    } = this.data;
    if (storeAllList.length > cityIndex[0] && storeAllList[cityIndex[0]].storeList.length > cityIndex[1]) {
      let item = storeAllList[cityIndex[0]].storeList[cityIndex[1]];
      this.setData({
        institution_name: item ? item.name : '',
        store_id: item ? item.storeId : '',
      })
    }
  },
  async patientStortList() {
    let {
      data
    } = await api.patientStortList();
    let list = data && Array.isArray(data.list) ? data.list : [];
    this.setData({
      agencyList: list
    })
    this.getAreaList();
  },
  submit() {
    if (!this.data.nickname || this.data.nickname == '') return wx.showToast({
      title: '请填写姓名',
      icon: 'none'
    })
    if (!util.regularConfig.mobile.test(this.data.mobile)) return wx.showToast({
      title: '电话格式错误',
      icon: 'none'
    })
    if (!this.data.store_id || this.data.store_id == '') return wx.showToast({
      title: '投诉机构格式错误',
      icon: 'none'
    })
    if (!this.data.complaint || this.data.complaint == '') return wx.showToast({
      title: '请填写投诉内容',
      icon: 'none'
    })
    api.setComplaint({
      name: this.data.nickname,
      mobile: this.data.mobile,
      complaint: this.data.complaint,
      opinion: this.data.suggestion,
      store_id:this.data.store_id
    }).then(res => {
      wx.showToast({
        title: '提交成功',
        icon: 'success'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    return util.shareEvent(e)
  }
})